// 
// SoapTrace.Net - A soap message monitoring utility. 
// 
// Copyright (C) 2004 Matthew Ward
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
// 
// Matthew Ward (mrward@users.sourceforge.net)
// 

using SoapTrace.Core;
using SoapTrace.Core.Diagnostics;
using SoapTrace.Core.Formatters; 
using System;
using System.Windows.Forms;

namespace SoapTrace
{
	/// <summary>
	/// The controller for the Soap Trace application options form.
	/// </summary>
	public class SoapTraceOptionsEditor : IDisposable
	{
		/// <summary>
		/// Disposed flag.
		/// </summary>
		private bool m_Disposed;

		/// <summary>
		/// The collection of message formatters.
		/// </summary>
		private MessageFormattersCollection m_Formatters;

		/// <summary>
		/// A reference to the options form.
		/// </summary>
		private OptionsForm m_OptionsForm;
		
		/// <summary>
		/// A reference to the logging manager.
		/// </summary>
		private SoapMessageLoggingManager m_LoggingManager;
		
		/// <summary>
		/// A reference to the main form.
		/// </summary>
		private MainForm m_MainForm;
		
		/// <summary>
		/// Creates a new instance of this class.
		/// </summary>
		/// <param name="formatters">The message formatters to show
		/// options for.</param>
		/// <param name="loggingManager">The logging manager.</param>
		/// <param name="mainForm">The main form.</param>
		public SoapTraceOptionsEditor( MessageFormattersCollection formatters, SoapMessageLoggingManager loggingManager, MainForm mainForm )
		{
			// Save a reference to the message formatters so we
			// can configure them.
			m_Formatters = formatters;
			
			// Create options form.
			m_OptionsForm = new OptionsForm( this );

			// Disable the Options button.
			m_OptionsForm.FormatterOptionsButtonEnabled = false;

			// Add the formatter names to the formatters list box.
			AddFormatterNames( );
			
			// Set up logging info.
			m_LoggingManager = loggingManager;
			m_OptionsForm.LoggingEnabled = m_LoggingManager.LoggingEnabled;
			m_OptionsForm.LogFolder = m_LoggingManager.LogFolder;
			
			// General options.
			m_MainForm = mainForm;
			m_OptionsForm.ShowInTray = m_MainForm.ShowInTray;
		}

		/// <summary>
		/// Disposes this class.
		/// </summary>
		public void Dispose( )
		{
			Dispose( true );

			GC.SuppressFinalize( this );
		}
		
		/// <summary>
		/// Shows the application options.
		/// </summary>
		public void Show( )
		{
			m_OptionsForm.ShowDialog( m_MainForm );
		}

		/// <summary>
		/// Saves the changes made in the options dialog.
		/// </summary>
		public void SaveChanges( )
		{
			m_LoggingManager.LoggingEnabled = m_OptionsForm.LoggingEnabled;
			m_LoggingManager.LogFolder = m_OptionsForm.LogFolder;
			m_MainForm.ShowInTray = m_OptionsForm.ShowInTray;
		}

		/// <summary>
		/// Ignores all the changes made in the options dialog.
		/// </summary>
		public void UndoChanges( )
		{
		}

		/// <summary>
		/// Shows the formatter configurable options.
		/// </summary>
		/// <param name="name">The name of the formatter.</param>
		public void ShowFormatterOptions( string name )
		{
			IMessageFormatter Formatter = m_Formatters[ name ];
			IConfigurationEditor ConfigEditor = Formatter.ConfigurationEditor;
			ConfigEditor.Show( );
		}

		/// <summary>
		/// Handles the event where a formatter has been selected by the user.
		/// </summary>
		/// <param name="name">The name of the formatter.</param>
		public void FormatterSelected( string name )
		{
			if( name != null )
			{
				// Enable the options button if the formatter is
				// configurable.

				m_OptionsForm.FormatterOptionsButtonEnabled = 
					m_Formatters.IsConfigurable( name );
			}
			else
			{
				m_OptionsForm.FormatterOptionsButtonEnabled = false;
			}
		}
		
		/// <summary>
		/// Browses for the logging folder.
		/// </summary>
		public void BrowseForLoggingFolder( )
		{
			using( FolderBrowserDialog FolderDialog = new FolderBrowserDialog( ) )
			{
				FolderDialog.Description = "Select folder for log files.";
				
				DialogResult Result = FolderDialog.ShowDialog( );
				
				if( Result == DialogResult.OK )
				{
					m_OptionsForm.LogFolder = FolderDialog.SelectedPath;
				}
			}
		}

		/// <summary>
		/// Gets the message formatter names as a string array.
		/// </summary>
		/// <returns>A string array of formatter names.</returns>
		private string[ ] GetFormatterNames( )
		{
			string[ ] Names = new string[ m_Formatters.Count ];

			for( int i = 0; i < m_Formatters.Count; ++i )
			{
				Names[ i ] = m_Formatters[ i ].Name;
			}

			return Names;
		}

		/// <summary>
		/// Adds the formatter names to the formatters list box.
		/// </summary>
		private void AddFormatterNames( )
		{
			string[ ] FormatterNames = GetFormatterNames( );
			m_OptionsForm.AddFormatters( FormatterNames );
		}
		
		/// <summary>
		/// Disposes managed and unmanaged objects. 
		/// </summary>
		/// <param name="disposing">If <see langword="true"/> the method has been called directly
		/// or indirectly by a user's code. Managed and unmanaged resources
		/// can be disposed.
		/// If disposing equals <see langword="false"/>, the method has been called by the 
		/// runtime from inside the finalizer and you should not reference 
		/// other objects. Only unmanaged resources can be disposed.
		/// </param>
		private void Dispose( bool disposing )
		{
			if( !m_Disposed )
			{
				if( disposing )
				{
					// Dispose managed resources.
					
					if( m_OptionsForm != null )
					{
						m_OptionsForm.Dispose( );
					}
				}
			}

			m_Disposed = true;         
		}
		
	}
}
